﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OSIRIS
{
    /// <summary>
    /// The OSIRISData class emulates the data layer of OSIRIS and fetches
    /// details from the database.
    /// </summary>
    public class OSIRISData
    {
        /// <summary>
        /// Default constructor.
        /// </summary>
        public OSIRISData()
        {

        }

        /// <summary>
        /// Check if a certain student is allowed to go on final internship.
        /// </summary>
        /// <param name="number">The number of the student</param>
        /// <returns>True if allowed, false if not</returns>
        public bool AskForPermission(string number)
        {
            /* fetch student from database */
            Student student = this.GetStudent(number);

            /* define counter */
            int pointCount = 0;

            /* if a student was found */
            if (student != null)
            {
                /* add points each result */
                foreach (Result result in student.Results)
                {
                    pointCount += result.Points;
                }
            }

            /* ask the properties file for the permission boundary */
            int permissionBoundary = global::OSIRIS.Properties.Settings.Default.PermissionBoundary;

            /* return whether or not the points exceed the boundary */
            return (pointCount >= permissionBoundary);
        }

        /// <summary>
        /// Fetch a Student object from the database.
        /// </summary>
        /// <param name="number">The number of the student</param>
        /// <returns>The initialized Student object</returns>
        public Student GetStudent(string number)
        {
            /* declare Student variable */
            Student result = null;

            /* find Student object */
            OSIRISDataContext database = new OSIRISDataContext();
            IEnumerable<Student> foundStudents =
                (from student in database.Students
                where student.Number == number
                select student);

            /* if the Student is not null */
            if (foundStudents.Count() > 0)
            {
                /* get the first in the result set */
                result = foundStudents.First();
            }

            /* close database connection */
            database.Connection.Close();

            /* return the result */
            return result;
        }

        /// <summary>
        /// Fetch a Teacher object from the database.
        /// </summary>
        /// <param name="number">The number of the teacher</param>
        /// <returns>The initialized Teacher object</returns>
        public Teacher GetTeacher(string number)
        {
            /* create Teacher object */
            Teacher result = null;

            /* find Teacher in the database */
            OSIRISDataContext database = new OSIRISDataContext();
            IEnumerable<Teacher> foundTeachers =
                (from teacher in database.Teachers
                 where teacher.Number == number
                 select teacher);

            /* if the Teacher is not null */
            if (foundTeachers.Count() > 0)
            {
                /* get first Teacher from result set */
                result = foundTeachers.First();
            }

            /* close database connection */
            database.Connection.Close();

            /* return the result */
            return result;
        }
    }
}
